#!/bin/sh

# run from directory where this script is
cd `echo $0 | sed 's/\(.*\)\/.*/\1/'` # extract pathname
EXAMPLE_DIR=`pwd`

# check whether echo has the -e option
if test "`echo -e`" = "-e" ; then ECHO=echo ; else ECHO="echo -e" ; fi

$ECHO
$ECHO "$EXAMPLE_DIR : starting"
$ECHO
$ECHO "This example shows how to calculate the total energy of an isolated"
$ECHO "atom in a supercell with fixed occupations."
$ECHO "Two examples: LDA energy of Al and sigma-GGA energy of O."

# set the needed environment variables
. ../../../environment_variables

# required executables and pseudopotentials
BIN_LIST="pw.x"
PSEUDO_LIST=" Al.pz-vbc.UPF O.pbe-rrkjus.UPF"

$ECHO
$ECHO "  executables directory: $BIN_DIR"
$ECHO "  pseudo directory:      $PSEUDO_DIR"
$ECHO "  temporary directory:   $TMP_DIR"
$ECHO "  checking that needed directories and files exist...\c"

# check for directories
for DIR in "$BIN_DIR" "$PSEUDO_DIR" ; do
    if test ! -d $DIR ; then
        $ECHO
        $ECHO "ERROR: $DIR not existent or not a directory"
        $ECHO "Aborting"
        exit 1
    fi
done
for DIR in "$TMP_DIR" "$EXAMPLE_DIR/results" ; do
    if test ! -d $DIR ; then
        mkdir $DIR
    fi
done
cd $EXAMPLE_DIR/results

# check for executables
for FILE in $BIN_LIST ; do
    if test ! -x $BIN_DIR/$FILE ; then
        $ECHO
        $ECHO "ERROR: $BIN_DIR/$FILE not existent or not executable"
        $ECHO "Aborting"
        exit 1
    fi
done

# check for pseudopotentials
for FILE in $PSEUDO_LIST ; do
    if test ! -r $PSEUDO_DIR/$FILE ; then
       $ECHO
       $ECHO "Downloading $FILE to $PSEUDO_DIR...\c"
            $WGET $PSEUDO_DIR/$FILE \
                http://www.quantum-espresso.org/pseudo/1.3/UPF/$FILE 2> /dev/null
    fi
    if test $? != 0; then
        $ECHO
        $ECHO "ERROR: $PSEUDO_DIR/$FILE not existent or not readable"
        $ECHO "Aborting"
        exit 1
    fi
done
$ECHO " done"

# how to run executables
PW_COMMAND="$PARA_PREFIX $BIN_DIR/pw.x $PARA_POSTFIX"
$ECHO
$ECHO "  running pw.x as: $PW_COMMAND"
$ECHO

# clean TMP_DIR
$ECHO "  cleaning $TMP_DIR...\c"
rm -rf $TMP_DIR/*
$ECHO " done"

# self-consistent calculation for isolated Al atom
cat > al.xml << EOF
<?xml version="1.0" encoding="UTF-8"?>


<input calculation="scf">

	<cell type="qecell">
		<qecell ibrav="1" alat="20.0">
			<real rank="1" n1="5">
				0.0 0.0 0.0 0.0 0.0
			</real>
		</qecell>
	</cell>

	<atomic_species ntyp="1">
		<specie name="Al">
			<property name="mass">
				<real>26.98154</real>
			</property>
			<property name="pseudofile">
				<string>Al.pz-vbc.UPF</string>
			</property>
		</specie>
	</atomic_species>

	<atomic_list units="alat" nat="1" >
		<atom name="Al">
                     <position>
			<real rank="1" n1="3">
				0.0000000000   0.0000000000     0.000
			</real>
                     </position>
		</atom>						
	</atomic_list>		
	
	
	<field name="InputOutput">

		<parameter name="restart_mode">
			<string>
				from_scratch
			</string>
		</parameter>

		<parameter name="pseudo_dir">
			<string>
				$PSEUDO_DIR/
			</string>
		</parameter>
		
		<parameter name="outdir">
			<string>
				$TMP_DIR/
			</string>
		</parameter>
		
	</field>
	
	<field name="Numerics">

		<parameter name="ecutwfc">
			<real>
				15.0
			</real>
		</parameter>
		
		<parameter name="mixing_beta">
			<real>
				0.35
			</real>
		</parameter>
		
		<parameter name="conv_thr">
			<real>
				1.0E-8
			</real>
		</parameter>
		
		<parameter name="nosym">
			<logical>
				true
			</logical>
		</parameter>
	
	</field>
	
	<field name="Options">

		<parameter name="occupations">
			<string>
				from_input
			</string>
		</parameter>
		
		<parameter name="nbnd">
			<integer>
				6
			</integer>
		</parameter>
		
	</field>	
	
	<k_points type="automatic">
		<mesh>
			<integer rank="1" n1="6">
				1 1 1 0 0 0
			</integer>
		</mesh> 
	</k_points>
	
	<occupations>
		<real rank="1" n1="6">
				2.0 0.3333333333333 0.333333333333 0.3333333333333 0.0 0.0
		</real>	
	</occupations>
</input>
EOF
$ECHO "  running self-consistent calculation for Al atom...\c"
$PW_COMMAND < al.xml > al.out
check_failure $?
$ECHO " done"

# self consistent calculation for the spin polarized O atom
cat > O.xml << EOF
<?xml version="1.0" encoding="UTF-8"?>


<input calculation="scf">

	<cell type="qecell">
		<qecell ibrav="1" alat="14.0">
			<real rank="1" n1="5">
				0.0 0.0 0.0 0.0 0.0
			</real>
		</qecell>
	</cell>

	<atomic_species ntyp="1">
		<specie name="O">
			<property name="mass">
				<real>15.99994</real>
			</property>
			<property name="pseudofile">
				<string>O.pbe-rrkjus.UPF</string>
			</property>
			<property name="starting_magnetization">
				<real>0.5d0</real>
			</property>
		</specie>
	</atomic_species>

	<atomic_list units="alat" nat="1" >
		<atom name="O">
                     <position>
			<real rank="1" n1="3">
				0.000000000   0.000000000   0.000000000
			</real>
                     </position>
		</atom>
	</atomic_list>		
	
	
	<field name="InputOutput">

		<parameter name="restart_mode">
			<string>
				from_scratch
			</string>
		</parameter>

		<parameter name="pseudo_dir">
			<string>
				$PSEUDO_DIR/
			</string>
		</parameter>
		
		<parameter name="outdir">
			<string>
				$TMP_DIR/
			</string>
		</parameter>
		
	</field>
	
	<field name="Numerics">

		<parameter name="ecutwfc">
			<real>
				27.0
			</real>
		</parameter>
		
		<parameter name="ecutrho">
			<real>
				216.0
			</real>
		</parameter>
		
		<parameter name="mixing_beta">
			<real>
				0.25
			</real>
		</parameter>
		
		<parameter name="conv_thr">
			<real>
				1.0E-8
			</real>
		</parameter>
		
		<parameter name="nosym">
			<logical>
				true
			</logical>
		</parameter>
	
	</field>
	
	<field name="Options">

		<parameter name="occupations">
			<string>
				from_input
			</string>
		</parameter>
		
		<parameter name="nbnd">
			<integer>
				6
			</integer>
		</parameter>
		
	</field>
	
	<field name="Fields">

		<parameter name="nspin">
			<integer>
				2
			</integer>
		</parameter>
	
	</field>	
	
	<k_points type="automatic">
		<mesh>
			<integer rank="1" n1="6">
				1 1 1 0 0 0
			</integer>
		</mesh> 
	</k_points>
	
	<occupations>
		<real rank="2" n1="6" n2="2">
				1.0 1.0 1.0 1.0 0.0 0.0
				1.0 0.33333333333 0.33333333333 0.33333333333 0.0 0.0
		</real>
	</occupations>
</input>
EOF
$ECHO "  running calculation for O atom...\c"
$PW_COMMAND < O.xml > O.out
check_failure $?
$ECHO " done"

#
cat > O_gamma.xml << EOF
<?xml version="1.0" encoding="UTF-8"?>

<input calculation="scf">

	<cell type="qecell">
		<qecell ibrav="1" alat="14.0">
			<real rank="1" n1="5">
				0.0 0.0 0.0 0.0 0.0
			</real>
		</qecell>
	</cell>

	<atomic_species ntyp="1">
		<specie name="O">
			<property name="mass">
				<real>15.99994</real>
			</property>
			<property name="pseudofile">
				<string>O.pbe-rrkjus.UPF</string>
			</property>
			<property name="starting_magnetization">
				<real>0.5d0</real>
			</property>
		</specie>
	</atomic_species>

	<atomic_list units="alat" nat="1" >
		<atom name="O">
                     <position>
			<real rank="1" n1="3">
				0.000000000   0.000000000   0.000000000
			</real>
                     </position>
		</atom>
	</atomic_list>		
	
	
	<field name="InputOutput">

		<parameter name="restart_mode">
			<string>
				from_scratch
			</string>
		</parameter>

		<parameter name="pseudo_dir">
			<string>
				$PSEUDO_DIR/
			</string>
		</parameter>
		
		<parameter name="outdir">
			<string>
				$TMP_DIR/
			</string>
		</parameter>		
		
	</field>
	
	<field name="Numerics">

		<parameter name="ecutwfc">
			<real>
				27.0
			</real>
		</parameter>
		
		<parameter name="ecutrho">
			<real>
				216.0
			</real>
		</parameter>
		
		<parameter name="mixing_beta">
			<real>
				0.25
			</real>
		</parameter>
		
		<parameter name="conv_thr">
			<real>
				1.0E-8
			</real>
		</parameter>
		
		<parameter name="nosym">
			<logical>
				true
			</logical>
		</parameter>
		
	</field>
	
	<field name="Options">

		<parameter name="occupations">
			<string>
				from_input
			</string>
		</parameter>
		
		<parameter name="nbnd">
			<integer>
				6
			</integer>
		</parameter>		
		
	</field>	
	
	<field name="Fields">

		<parameter name="nspin">
			<integer>
				2
			</integer>
		</parameter>
	
	</field>
	
	<k_points type="gamma">		
	</k_points>	

	<occupations>
		<real rank="2" n1="6" n2="2">
			1.0 1.0 1.0 1.0 0.0 0.0
			1.0 0.33333333333 0.33333333333 0.33333333333 0.0 0.0
		</real>	
	</occupations>
</input>
EOF
$ECHO "  running calculation for O atom, Gamma-only...\c"
$PW_COMMAND < O_gamma.xml > O_gamma.out
check_failure $?
$ECHO " done"

$ECHO
$ECHO "$EXAMPLE_DIR: done"
